package com.example.day14;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;


public class MainActivity extends AppCompatActivity {
    //创建子线程
    class MyThread extends Thread {
        private Looper looper;//取出该子线程的Looper

        public void run() {

            Looper.prepare();//创建该子线程的Looper
            looper = Looper.myLooper();//取出该子线程的Looper
            Looper.loop();//只要调用了该方法才能不断循环取出消息
        }
    }

    private Handler mHandler;//将mHandler指定轮询的Looper

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化一个特殊的线程HandlerThread，必须给其指定一个名字
        HandlerThread thread = new HandlerThread("handler thread");
        thread.start();//千万不要忘记开启这个线程
        //将mHandler与thread相关联
        mHandler = new Handler(thread.getLooper()) {
            public void handleMessage(android.os.Message msg) {
                Log.d("当前子线程是----->", Thread.currentThread() + "");
            }
        };
        mHandler.sendEmptyMessage(1);//发送消息


    }
}
